package fc.sj.study;

import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;

import com.zhy.http.okhttp.OkHttpUtils;

import java.util.concurrent.TimeUnit;

import io.rong.imkit.RongIM;
import io.rong.imlib.RongIMClient;
import okhttp3.OkHttpClient;

/**
 * Created by Administrator on 2017/3/16.
 */

public class MApplication extends Application{
    private static MApplication app;
    @Override
    public void onCreate() {
        super.onCreate();
        app = this;
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
//                .addInterceptor(new LoggerInterceptor("TAG"))
                .connectTimeout(10000L, TimeUnit.MILLISECONDS)
                .readTimeout(10000L, TimeUnit.MILLISECONDS)
                //其他配置
                .build();
        OkHttpUtils.initClient(okHttpClient);
        RongIM.init(this);
    }
    public static Context getAppContext() {
        return app.getApplicationContext();
    }

    /**
     * 初始化融云即时通讯
     */
    private void initRongIm() {
        /**
         * OnCreate 会被多个进程重入，这段保护代码，确保只有您需要使用 RongIMClient 的进程和 Push 进程执行了 init。
         * io.rong.push 为融云 push 进程名称，不可修改。
         */
        if(getApplicationInfo().packageName.equals(
                getCurProcessName(getApplicationContext()))
                ||"io.rong.push".equals(
                getCurProcessName(getApplicationContext()))) {
            RongIMClient.init(this);
        }
    }

    /**
     * 获得当前进程的名字
     *
     * @param context context
     * @return 进程号
     */
    public static String getCurProcessName(Context context) {
        int pid = android.os.Process.myPid();
        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        for (ActivityManager.RunningAppProcessInfo appProcess : activityManager.getRunningAppProcesses()) {
            if (appProcess.pid == pid) {
                return appProcess.processName;
            }
        }
        return null;
    }


}
